#!/bin/bash

echo "🧪 快速测试线上聊天功能..."

# 测试WebSocket连接
echo "1. 测试WebSocket端点..."
response=$(curl -s -w "%{http_code}" https://c4pad.fun/socket.io/?EIO=4&transport=polling -o /tmp/websocket_test.txt 2>/dev/null)
if [ "$response" = "200" ]; then
    echo "   ✅ WebSocket端点响应正常"
    echo "   📄 响应内容: $(head -c 100 /tmp/websocket_test.txt)..."
else
    echo "   ❌ WebSocket端点响应异常: $response"
fi

# 测试后端API
echo "2. 测试后端API..."
health_response=$(curl -s https://c4pad.fun/api/health 2>/dev/null)
if [ $? -eq 0 ]; then
    echo "   ✅ 后端API正常"
    echo "   📄 健康检查: $health_response"
else
    echo "   ❌ 后端API异常"
fi

# 使用我们之前创建的WebSocket测试脚本
echo "3. 测试完整的聊天功能..."
if [ -f "test-chat-specific.js" ]; then
    echo "   🔄 运行聊天功能测试..."
    timeout 15 node test-chat-specific.js 2>/dev/null || echo "   ⏰ 测试超时或失败"
else
    echo "   ⚠️  聊天测试脚本不存在"
fi

echo ""
echo "📊 完成！如果看到用户加入和消息事件，说明聊天功能正常。"
echo "🌐 现在可以访问 https://c4pad.fun 验证聊天功能。"
